﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;
using System.Configuration;
using SouthApps.MobileHealth.SDK.Common.Exceptions;
using SouthApps.MobileHealth.SDK.DAL.Model;
using System.Data.Common;
using SouthApps.MobileHealth.SDK.Common;
using SouthApps.MobileHealth.SDK.DAL.DataAccess.MySql;

namespace SouthApps.MobileHealth.SDK.DAL.DataAccess
{
    public abstract class DataAccessObject: IDisposable
    {
        private bool isDisposed;
        private EntityConnection connection;

        protected MobileHealthEntities EntityContext
        {
            get
            {
                if (this.isDisposed)
                {
                    throw new ObjectDisposedException("The data access object has been disposed.");
                }

                try
                {
                    EntityConnection connection = new EntityConnection(ConfigurationManager.ConnectionStrings["MobileHealthEntities"].ConnectionString);
                    return new MobileHealthEntities(connection);
                }
                catch (Exception ex)
                {
                    throw new DataAccessException(DataAccessErrorCode.ConnectionConfigurationError, "Error creando conexion.", ex.Message);
                }
            }
        }

        public void Dispose()
        {
            this.Dispose(true);

            GC.SuppressFinalize(this);
        }


        protected virtual void Dispose(bool disposing)
        {
            if (this.connection != null)
            {
                this.connection.Dispose();
                this.connection = null;
            }

            this.isDisposed = true;
        }

        protected Exception HandleException(Exception ex)
        {
            if (ex is DbException)
            {
                DataAccessException dataAccessException = new DataAccessException();

                DbException dbException = (DbException)ex;

                switch (dbException.ErrorCode)
                {
                    case MySqlErrorCodes.UniqueKeyConstraint:
                        dataAccessException.DataBaseResultCode = DataAccessErrorCode.UniqueKeyConstraintViolation;
                        break;
                    case MySqlErrorCodes.ForeignKeyRowNotExists:
                    case MySqlErrorCodes.ForeignKeyRowReferenced:
                        dataAccessException.DataBaseResultCode = DataAccessErrorCode.ForeignKeyViolation;
                        break;
                    default:
                        dataAccessException.DataBaseResultCode = DataAccessErrorCode.UnknownError;
                        break;
                }

                return dataAccessException;
            }
            
            return ex;
        }

        protected void SaveChanges(MobileHealthEntities entities)
        {
            int affectedRows = entities.SaveChanges();

            if (affectedRows == 0)
            {
                throw new DataAccessException(DataAccessErrorCode.RecordNotFound);
            }
        }
    }
}
